//
//  PKHeadWebViewController.m
//  PianKe
//
//  Created by lanou on 15/10/14.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKHeadWebViewController.h"
#import "PKReadWebViewModel.h"
#import "PKRequestManager.h"
#import "NSString+Html.h"
#import "UMSocial.h"
@interface PKHeadWebViewController ()<UMSocialUIDelegate>
@property(nonatomic,strong)PKReadWebViewModel *model;
@property(nonatomic,strong)UIWebView *webView;
@end

@implementation PKHeadWebViewController

-(void)configureTopView
{
    UILabel *commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WITDTH/3, 20, 40, 20)];
//    commentLabel.backgroundColor = [UIColor redColor];
    commentLabel.text = [NSString stringWithFormat:@"%@", _model.counterList[@"comment"]] ;
    
    
    UILabel *likeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WITDTH/2, 20, 40, 20)];
    likeLabel.text = [NSString stringWithFormat:@"%@", _model.counterList[@"like"]];
    
    UIButton *AButton = [UIButton buttonWithType:UIButtonTypeCustom];
    AButton.frame = CGRectMake(280, 20, 20, 20);
//    AButton.backgroundColor = [UIColor whiteColor];
    [AButton setTitle:@"A" forState:UIControlStateNormal];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(330, 20, 20, 20);
    [shareButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_topView addSubview:commentLabel];
    [_topView addSubview:likeLabel];
    [_topView addSubview:AButton];
    [_topView addSubview:shareButton];
    
}


-(void)shareAction
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"561b7a31e0f55a68f3001d4f"
                                      shareText:_model.html
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,nil]
                                       delegate:self];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    _contentid = [_url substringFromIndex:17];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WITDTH, SCREEN_HEIGHT-70)];
    [self.view addSubview:_webView];
    
   
    
    
    
    
    
    [self requestData];
}

-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"Xpc9Qi21paxDC8Z8GMHjlzJxQMOvZODf82lqWWRicWP22NVKQxta6fo";
    parDic[@"contentid"] = _contentid;
    parDic[@"client"] = @"2";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"version"] = @"3.0.2";
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/article/info" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        _model = [[PKReadWebViewModel alloc]init];
        
        [_model setValuesForKeysWithDictionary:dic[@"data"]];


        NSString *HtmlString = [NSString importStyleWithHtmlString:_model.html];
        [_webView loadHTMLString:HtmlString baseURL:[NSURL URLWithString:[NSBundle mainBundle].bundlePath]];
        
//        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_model.shareinfo[@"url"]]]];
        
//        NSString *HtmlSting = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.shareinfo[@"url"]]] encoding:NSUTF8StringEncoding];
//        [_webView loadHTMLString:HtmlSting baseURL:nil];
        
        
        
        
        
        
         [self configureTopView];
        
        
        
    } error:^(NSError *error) {
        NSLog(@"");
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
